trigger trg_ReceiptObject on Receipt__c (after insert, after update)
{
	Map<Id,Receipt__c> receiptInvoiceMap = new Map<Id,Receipt__c>();
	
	if(Trigger.isInsert)
	{
		for(Receipt__c receiptObject : Trigger.New)
		{
			if(receiptObject.is_Cancelled_Receipt__c)
			{
				if(receiptObject.Cancellation_Reason__c != null && receiptObject.Related_Invoice__c != null)
				{
					receiptInvoiceMap.put(receiptObject.Related_Invoice__c, receiptObject);	
				}
				
			}
			
		}	
	}
	else if (Trigger.isUpdate)
	{
		for(Receipt__c receiptObject : Trigger.New)
		{
			if(Trigger.oldMap.get(receiptObject.Id).is_Cancelled_Receipt__c != receiptObject.is_Cancelled_Receipt__c)
			{
				if(receiptObject.is_Cancelled_Receipt__c)
				{
					if(receiptObject.Cancellation_Reason__c != null && receiptObject.Related_Invoice__c != null)
					{
						//Adding in the Map...
						receiptInvoiceMap.put(receiptObject.Related_Invoice__c, receiptObject);
						
					}
				}
			}
			
		}
		
	}
	
	
	
	//Now the logic begins for the Cancell Rule...
	if(receiptInvoiceMap.size() > 0)
	{
		//List<Invoice__c> invoiceList = new List<Invoice__c>([select id,Cancelled__c,Cancellation_Reason__c,Cancellation_Notes__c from Invoice__c where Id IN : receiptInvoiceMap.values()]);
		List<Invoice__c> invoiceList = new List<Invoice__c>();
		
		for(Invoice__c invoiceObject : [select id,Cancelled__c,Cancellation_Reason__c,Cancellation_Notes__c from Invoice__c where Id IN : receiptInvoiceMap.keyset()])
		{
			invoiceObject.Cancelled__c = receiptInvoiceMap.get(invoiceObject.Id).is_Cancelled_Receipt__c;
			invoiceObject.Cancellation_Reason__c = receiptInvoiceMap.get(invoiceObject.Id).Cancellation_Reason__c;
			invoiceObject.Cancellation_Notes__c = receiptInvoiceMap.get(invoiceObject.Id).Cancellation_Notes__c;
			
			//Adding in the List...
			invoiceList.add(invoiceObject);
		}	
	
		if(invoiceList.size() > 0)
			update invoiceList;
	
	
	}

}